﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InfoSource.Models.objects;

namespace InfoSource.Data
{
	public class TechnicianRequests
	{
		public static SeniorTechAndSpecialist GetSeniorAndSpecialistForZipcode(int zipcode)
		{
			InfoSource.Data.v_Ins_and_Sec_Technicians senior, specialist;

			InfoSourceDB db = new InfoSourceDB();
			var dbZip = db.colorado_zipcodes.Where(zip => zip.zip == zipcode).FirstOrDefault() ?? new InfoSource.Data.colorado_zipcode();
			var seniorZone = db.v_Ins_and_Sec_tbl_SeniorZones.Where(snr => snr.SeniorZoneID == dbZip.SeniorZoneID).FirstOrDefault();
			var specialistZone = db.v_Ins_and_Sec_tbl_SpecialistZones.Where(spec => spec.SpecialistZoneID == dbZip.SpecialistZoneID).FirstOrDefault();

			if (seniorZone != null)
			{
				senior = db.v_Ins_and_Sec_Technicians.Where(tech => tech.Tech_ID == seniorZone.TechID).FirstOrDefault();
			}
			else
			{
				senior = new InfoSource.Data.v_Ins_and_Sec_Technicians() { Tech_Name = "", };
			}

			if (specialistZone != null)
			{
				specialist = db.v_Ins_and_Sec_Technicians.Where(tech => tech.Tech_ID == specialistZone.TechID).FirstOrDefault();
			}
			else
			{
				specialist = new InfoSource.Data.v_Ins_and_Sec_Technicians() { Tech_Name = "" };
			}


			return new SeniorTechAndSpecialist()
			{
				Senior = new SimpleTechnician()
				{
					Code = dbZip.SeniorZoneID,
					Name = senior.Tech_Name
				},
				Specialist = new SimpleTechnician()
				{
					Code = dbZip.SpecialistZoneID,
					Name = specialist.Tech_Name
				}
			};
		}

	}
}